# -*- coding: utf-8 -*-
import PySimpleGUI as sg
import os


def traverse_files(folder_path):
    file_list = []
    for root, dirs, files in os.walk(folder_path):
        for the_file in files:
            file_list.append(os.path.join(root, the_file))
    return file_list


layout = [
    [sg.Text(u'by火鸡科学家')],
    [sg.HorizontalSeparator()],
    [sg.InputText(u'所选文件路径', size=(60, 2), key='-FILEPATH-')],
    [sg.FolderBrowse(u"请选择文件夹", target='-GETFILE-', key='-GETFILE-',
                     enable_events=True, font=16, size=(60, 2))],
    [sg.Button(u'动手！', font=16, size=(10, 2), key='-GENERATE-'), sg.Button(u'退出', font=16, size=(10, 2))],
    [sg.HorizontalSeparator()],
    [sg.Multiline('', size=(700, 400), key='-OUTPUT-')],
]

# Create the Window
window = sg.Window('删除重复文件小tool', layout, size=(900, 700),resizable=True)


# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == '退出':  # if user closes window or clicks cancel
        break
    elif event == '-GETFILE-':
        name = values['-GETFILE-']
        window['-FILEPATH-'].update(name)
    elif event == '-GENERATE-':
        if os.path.exists(values['-FILEPATH-']):
            target_list = traverse_files(values['-FILEPATH-'])
            log_list=''
            for eBook in target_list:
                if eBook.endswith('(1).mobi') or eBook.endswith('(1).azw3'):
                    if os.path.exists(eBook):
                        log_list+=eBook+'\n'
                        window['-OUTPUT-'].update(log_list)
                        # print(eBook)
                        os.remove(eBook)

window.close()

# target_path=r'F:\Books\toRead\学习\2 Books\0 在看\ForKindle\待定'
